iT邦幫忙

2022 iThome 鐵人賽

DAY 7
0

In this exercise you need to implement some functions to manipulate a list of programming languages.

1. Define a function to return an empty language list

Define the new/0 function that takes no arguments and returns an empty list.

2. Define a function to add a language to the list

Define the add/2 function that takes 2 arguments (a language list and a string literal of a language). It should return the resulting list with the new language prepended to the given list.

3. Define a function to remove a language from the list

Define the remove/1 function that takes 1 argument (a language list). It should return the list without the first item. Assume the list will always have at least one item.

4. Define a function to return the first item in the list

Define the first/1 function that takes 1 argument (a language list). It should return the first language in the list. Assume the list will always have at least one item.

5. Define a function to return how many languages are in the list

Define the count/1 function that takes 1 argument (a language list). It should return the number of languages in the list.

6. Define a function to determine if the list includes a functional language

Define the functional_list?/1 function which takes 1 argument (a language list). It should return a boolean value. It should return true if "Elixir" is one of the languages in the list.


https://exercism.org/tracks/elixir/exercises/language-list

defmodule LanguageList do
  def new() do
    []
  end

  def add(list, language) do
    [language | list]
  end

  def remove(list) do
    [_ | tail] = list
    tail
  end

  def first(list) do
    [head | _] = list
    head
  end

  def count(list) do
    length list
  end

  def functional_list?(list) do
    "Elixir" in list
  end
end

上一篇
六. Atom & Cond
下一篇
八.Multiple Clause Functions
系列文
戀戀 Elixir30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言